﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace D2DEngine.ParticleEffect
{
    /// <summary>
    /// A group of particle effects
    /// </summary>
    public class D2DParticleSystem : D2DSceneObject
    {
        protected List<D2DParticleEffect> effects;
        protected Dictionary<string, float> effectDelays;

        public D2DParticleSystem()
        {
            effects = new List<D2DParticleEffect>();
            effectDelays = new Dictionary<string, float>();

        }

        /// <summary>
        /// Adds an effect to the particle system
        /// </summary>
        /// <param name="effect">The particle effect</param>
        public void AddEffect(D2DParticleEffect effect)
        {
            this.effects.Add(effect);
            this.effectDelays.Add(effect.Name, 0.0f);
        }

        /// <summary>
        /// Adds an effect to the particle system
        /// </summary>
        /// <param name="effect">The particle effect</param>
        /// <param name="delay">The spawn delay from the beginning</param>
        public void AddEffect(D2DParticleEffect effect, float delay)
        {
            this.effects.Add(effect);
            this.effectDelays.Add(effect.Name, delay);
        }

        /// <summary>
        /// Code to execute when the system is
        /// registered to a scene.
        /// </summary>
        public override void OnRegister()
        {
            foreach (D2DParticleEffect effect in this.effects)
            {
                effect.OnRegister();
            }
            
            base.OnRegister();
        }

        /// <summary>
        /// Spawns the particle system
        /// </summary>
        public void Spawn()
        {
            foreach (D2DParticleEffect effect in this.effects)
            {
                effect.Position = Position;
                effect.Spawn();
            }
        }

        /// <summary>
        /// Updates the particle system
        /// </summary>
        /// <param name="gameTime">The game time</param>
        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            foreach (D2DParticleEffect effect in this.effects)
            {
                effect.Update(gameTime);
            }

            base.Update(gameTime);
        }

        /// <summary>
        /// Draws the particle system
        /// </summary>
        /// <param name="spriteBatch">The sprite batch</param>
        public override void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
        {
            foreach (D2DParticleEffect effect in this.effects)
            {
                effect.Draw(spriteBatch);
            }
            
            base.Draw(spriteBatch);
        }

    }
}
